package com.java.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.java.model.*;
import com.java.service.*;
import com.java.utils.CommonUtils;
import com.java.utils.JsonBody;
import com.java.utils.PageList;
import com.java.utils.ResponseUtil;;

@RestController
@RequestMapping("/buylist")
public class BuyListController {
	@Autowired
	private BuyService buyService;
	@Autowired
	private BuyListService buylistService;
	@Autowired
	private ThingService thingservice;
	@Autowired
	private ThingTypeService thingtypeService;
	@Autowired
	private ClientService clientService;
	@Autowired
	private ThingType2Service thingtype2Service;

	@Autowired
	private HttpServletRequest request;
	@Autowired
	private HttpServletResponse response;

	@CrossOrigin(origins = "*")
	@RequestMapping("/list")
	@ResponseBody
	public JsonBody Get(@RequestBody BuyList bl) {
		Map<String,Object> map =new HashMap<String,Object>();
		Buy buy=new Buy();
		buy.setId(bl.getBuyId());
		Buy b=buyService.GetByID(buy);
		b.setClient(clientService.GetByID(b.getClientId()));
			List<BuyList> list = buylistService.Get(bl);
			for(int i=0;i<list.size();i++)
			{
				Thing t=thingservice.GetByID(list.get(i).getThingId());
				ThingType tt=thingtypeService.GetByID(t.getThingtypeId());
				ThingType2 tt2=thingtype2Service.GetByID(t.getThingtype2Id());
				t.setThingtype(tt);
				t.setThingtype2(tt2);
				list.get(i).setThing(t);
			}
			map.put("buylist", list);
			map.put("buy", b);
			return Util.SetData(map);
	}




}
